
package com.fricano.java42.swing.dings.tester;

import java.awt.Rectangle;
import java.awt.Robot;
import java.awt.event.InputEvent;

import com.fricano.java42.base.components.utility.impl.Utility;
import com.fricano.java42.swing.dings.components.impl.J42SwingDing_BrokenWindow;
import com.fricano.java42.swing.dings.components.impl.J42SwingDing_ScreenWalker;
import com.fricano.java42.swing.dings.components.impl.J42SwingDing_ScreenWalker.J42ScreenWalker_Manager;

public class J42SwingDing_ScreenWalkerTest02 {

    private final Robot                     robot   = new Robot();
    private final J42SwingDing_ScreenWalker screenWalker;
    private boolean                         isBroke = false;

    public J42SwingDing_ScreenWalkerTest02() throws Exception {
        screenWalker = new J42SwingDing_ScreenWalker() {
            @Override
            public void progress(final J42ScreenWalker_Manager manager, final Rectangle bounds) {
                if (!isBroke && bounds.getX() > 100) {
                    isBroke = true;
                    Utility.sleep(1000);
                    final Thread t = new Thread(new Runnable() {
                        @Override
                        public void run() {
                            try {
                                main();
                            } catch (final Exception e) {
                                e.printStackTrace();
                            }
                        }
                    });
                    t.setDaemon(true);
                    t.start();
                }
            }
        };
    }

    public static void main(final String[] args) throws Exception {

        final J42SwingDing_ScreenWalkerTest02 tester = new J42SwingDing_ScreenWalkerTest02();

        final J42ScreenWalker_Manager manager = tester.screenWalker.getJ42ScreenWalker_Manager();
        manager.setElevationPercent(50);
        manager.setDurationMS(3000);
        manager.setImageName("images/film_640x440.png");
        manager.setImageName("images/walkingElephant.gif");
        manager.setImageSize(0, 350);
        manager.setAudioName("audio/elephant.wav");
        manager.setGifFrameDelayMS(50);
        manager.setPixelStep(8);
        tester.screenWalker.startWalking(manager);

        //tester.main();
    }

    public void main() throws Exception {

        //            while (screenWalker.getJ42ScreenWalker_Manager().getXProgress() < .125f) {
        //                Thread.sleep(100);
        //        }

        final J42SwingDing_BrokenWindow brokenWindow = new J42SwingDing_BrokenWindow();

        new Thread(new Runnable() {
            @Override
            public void run() {
                try {
                    while (!brokenWindow.isVisible()) {
                        Thread.sleep(1);
                    }
                    Thread.sleep(500);
                    robot.mousePress(InputEvent.BUTTON1_DOWN_MASK);
                    robot.mouseRelease(InputEvent.BUTTON1_DOWN_MASK);
                    robot.mousePress(InputEvent.BUTTON2_DOWN_MASK);
                    robot.mouseRelease(InputEvent.BUTTON2_DOWN_MASK);
                    screenWalker.toFront();
                } catch (final Exception e) {
                    e.printStackTrace();
                }
            }
        }).start();

        brokenWindow.getManager().activate();
        brokenWindow.getManager().waitForUserActivity(true);
        brokenWindow.getManager().deactivate(true);

    }

}
